package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-07
 * Time:14:40
 */
public class LoginPage extends Utils {
    public static String url = "http://81.70.248.108:58080/sign-in.html";

    public LoginPage() {
        super(url);
    }

    /**
     * 登录页面可以正常打开检查页面元素
     */
    public void checkLogin() throws InterruptedException {
        webDriver.manage().window().maximize();
        Thread.sleep(3000);
        //检查登录框
        webDriver.findElement(By.cssSelector("#username"));
        webDriver.findElement(By.cssSelector("#password"));
        webDriver.findElement(By.cssSelector("#submit"));
        webDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/div/div[3]/a"));

        //查看页面是正确
        String title = webDriver.getTitle();
        assert title.equals("论坛系统_用户登录"):"页面标题不对";
    }

    /**
     * 异常登录
     */
    public void loginFail(){
        //获取输入框，登录按钮
        By username = By.cssSelector("#username");
        By password = By.cssSelector("#password");
        By submit = By.cssSelector("#submit");
        //1.用户名和用户密码为空

        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(password).clear();
        //等待按钮可点击
        WebDriverWait buttonWait1 = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        buttonWait1.until(ExpectedConditions.elementToBeClickable(submit));
        webDriver.findElement(submit).click();


        //2.用户名错误，密码正确
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(password).clear();

        //输入用户名和密码
        webDriver.findElement(username).sendKeys("测试");
        webDriver.findElement(password).sendKeys("123456");
        //等待按钮可点击
        WebDriverWait buttonWait2 = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        buttonWait2.until(ExpectedConditions.elementToBeClickable(submit));
        webDriver.findElement(submit).click();


        //3.用户名正确，密码错误
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(password).clear();

        //输入用户名和密码
        webDriver.findElement(username).sendKeys("测试用户");
        webDriver.findElement(password).sendKeys("1234");
        //等待按钮可点击
        WebDriverWait buttonWait3 = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        buttonWait3.until(ExpectedConditions.elementToBeClickable(submit));
        webDriver.findElement(submit).click();


        //用户名和用户密码都错误
        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(password).clear();

        //输入用户名和密码
        webDriver.findElement(username).sendKeys("测试");
        webDriver.findElement(password).sendKeys("1234");
        //等待按钮可点击
        WebDriverWait buttonWait4 = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        buttonWait4.until(ExpectedConditions.elementToBeClickable(submit));
        webDriver.findElement(submit).click();


        //5.用户民为空，密码不为空
        webDriver.findElement(username).clear();
        webDriver.findElement(password).clear();

        //输入用户名和密码
        webDriver.findElement(password).sendKeys("1234");
        //等待按钮可点击
        WebDriverWait buttonWait5 = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        buttonWait5.until(ExpectedConditions.elementToBeClickable(submit));
        webDriver.findElement(submit).click();

        //6.用户名不为空，密码为空
        webDriver.findElement(username).clear();
        webDriver.findElement(password).clear();

        //输入用户名和密码
        webDriver.findElement(username).sendKeys("测试");
        //等待按钮可点击
        WebDriverWait buttonWait6 = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        buttonWait6.until(ExpectedConditions.elementToBeClickable(submit));
        webDriver.findElement(submit).click();
    }

    /**
     * 正常登录
     */
    public void loginSuc(String username1,String password1){
        //获取输入框，登录按钮
        By username = By.cssSelector("#username");
        By password = By.cssSelector("#password");
        By submit = By.cssSelector("#submit");
        //1.用户名和用户密码为空

        //清空输入框
        webDriver.findElement(username).clear();
        webDriver.findElement(password).clear();

        //输入正确的用户名和密码

        webDriver.findElement(username).sendKeys(username1);
        webDriver.findElement(password).sendKeys(password1);
        //等待按钮可点击
        WebDriverWait buttonWait1 = new WebDriverWait(webDriver, Duration.ofSeconds(3));
        buttonWait1.until(ExpectedConditions.elementToBeClickable(submit));
        webDriver.findElement(submit).click();

    }
}
